package com.interview.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(
        description = "通用返回响应结果对象",
        value = "result"
)
public class Result<T> implements Serializable {
    @ApiModelProperty(
            notes = "响应结果的错误信息",
            required = true
    )
    private String message;
    @ApiModelProperty(
            notes = "响应结果的错误信息状态码,2000X表示成功,4000X表示错误，5000X表示服务端异常",
            required = true
    )
    private Integer code;
    @ApiModelProperty(
            notes = "响应结果逻辑数据",
            required = true
    )
    private T data;

    public static <T> Result<T> ok() {
        return new Result(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code(), (Object)null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code(), data);
    }

    public static <T> Result<T> error() {
        return new Result(StatusCode.FAILURE.message(), StatusCode.FAILURE.code(), (Object)null);
    }

    public static <T> Result<T> error(T data) {
        return new Result(StatusCode.FAILURE.message(), StatusCode.FAILURE.code(), data);
    }

    public static <T> Result<T> error(StatusCode statusCode, T data) {
        return new Result(statusCode.message(), statusCode.code(), data);
    }

    public static <T> Result<T> error(StatusCode statusCode) {
        return new Result(statusCode.message(), statusCode.code(), (Object)null);
    }

    public static <T> Result<T> errorMessage(String message, Integer code, T data) {
        return new Result(message, code, data);
    }

    public static <T> Result<T> errorMessage(String message) {
        return new Result(message, StatusCode.CUSTOM_FAILURE.code(), (Object)null);
    }

    public Result() {
    }

    public Result(String message, Integer code, T data) {
        this.message = message;
        this.code = code;
        this.data = data;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean isSuccess() {
        return this.code.toString().startsWith("2000") ? true : false;
    }
}
